package com.windward.vii.service;

import com.windward.vii.dao.entity.Goods;
import com.windward.vii.dao.mapper.GoodsMapper;
import com.windward.vii.domain.goods.GoodsVO;
import com.windward.vii.utils.StringUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

@Component
public class GoodsService {

    @Resource
    GoodsMapper goodsMapper;

    public Goods getGoodsById(Long goodsId) {
        if(goodsId == null || goodsId <= 0) {
            return null;
        }
        return goodsMapper.selectByPrimaryKey(goodsId);
    }

    public Goods getBySn(String sn) {
        if(StringUtils.isBlank(sn)) {
            return null;
        }
        return goodsMapper.getBySn(sn);
    }


    public List<GoodsVO> getGoodsList(Integer terminalType) {
        List<Goods> goodsList = goodsMapper.getGoodsList(terminalType);
        List<GoodsVO> goodsVOList = new ArrayList<>();
        if(goodsList != null && goodsList.size() > 0) {
            for(Goods goods : goodsList) {
                try {
                    GoodsVO goodsVO = new GoodsVO();
                    BeanUtils.copyProperties(goodsVO, goods);
                    goodsVOList.add(goodsVO);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
        return goodsVOList;
    }

}
